package cn.netty.example1;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * <p>Title: Server</p>
 * <p>Description:Netty服务端</p>
 * @version V1.0
 * @author ZhiPeng_Chen
 * @date: 2018/10/26
 */
public class Server {

    //监听线程组
    private EventLoopGroup acceptorGroup;

    //处理客户端请求线程组
    private EventLoopGroup clientGroup;

    //服务器启动配置相关信息
    private ServerBootstrap serverBootstrap;

    //初始化netty配置
    public Server(){
        init();
    }

    private void init(){
        acceptorGroup = new NioEventLoopGroup();
        clientGroup = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();

        //绑定线程组
        serverBootstrap.group(acceptorGroup,clientGroup);
        //NIO通讯模式
        serverBootstrap.channel(NioServerSocketChannel.class);
        //缓冲区大小
        serverBootstrap.option(ChannelOption.SO_BACKLOG,1024);
        // 发送缓冲区、接收缓冲区、开启心跳检测
        serverBootstrap.option(ChannelOption.SO_SNDBUF,16*1024)
                .option(ChannelOption.SO_RCVBUF,16 * 1024)
                .option(ChannelOption.SO_KEEPALIVE,true);
    }


    /**
     * 监听处理逻辑
     * @param port 监听端口
     * @param acceptChannelHandler  监听处理器
     * @return
     * @throws InterruptedException
     */
    public ChannelFuture doAccept(int port, final ChannelHandler ... acceptChannelHandler) throws InterruptedException {


        /**
         * ChannelInitializer 提供处理器的一个模型对象
         */
        serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

            /**
             * initChannel 是服务端独有的方法,用于处理客户端的对象
             * initChannel 用于一次初始化处理逻辑的责任链条
             * @param ch
             * @throws Exception
             */
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                ch.pipeline().addLast(acceptChannelHandler);
            }
        });

        /**
         * bind绑定端口、可以绑定多个
         * sync 开始监听
         */
        ChannelFuture future = serverBootstrap.bind(port).sync();
        return future;
    }


    /**
     * 回收资源
     */
    public void release(){
        this.clientGroup.shutdownGracefully();
        this.acceptorGroup.shutdownGracefully();
    }


    public static void main(String[] args) {
        Server server = null;
        ChannelFuture future = null;
        try {
             server = new Server();
             future = server.doAccept(8080, new ServerHandler());
              System.out.println("Server-Start");
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            if (null != future){
                try {
                    future.channel().closeFuture().sync();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            if (null!=server) server.release();
        }
    }
}
